package com.seagull.core.test.io.aio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.Future;

/**
 * @author:jian.zhou
 * @DATE: 2018/1/3.
 * @Version:0.0.1
 * @Desc:
 */
public class AioClient {


    private AsynchronousSocketChannel client;

    public AioClient ( String ip ,int port ) throws Exception{
        client = AsynchronousSocketChannel.open();

        Future<?>  future = client.connect(new InetSocketAddress(ip,port));

        System.out.println(future.get());
    }

    public void write ( byte b ){
        ByteBuffer byteBuffer = ByteBuffer.allocate(32);
        byteBuffer.clear();
        byteBuffer.put(b);
        byteBuffer.flip();
        client.read(byteBuffer);
    }

    public static void main(String[] args) {
        try {
            AioClient aioClient = new AioClient("127.0.0.1",7080);
            aioClient.write((byte) 11111);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
